<template>
  <div class="product-list">
    <div class="page-header">
      <h1>产品列表</h1>
      <div class="cart-indicator">
        <router-link :to="{ name: 'Cart' }" class="cart-link">
          🛒 购物车 ({{ cartStore.totalCount }})
        </router-link>
      </div>
    </div>

    <div v-if="productStore.loading" class="loading">加载产品中...</div>

    <div v-else-if="productStore.errorMsg" class="error">
      {{ productStore.errorMsg }}
    </div>

    <div v-else class="products-grid">
      <ProductCard v-for="product in productStore.products" :key="product.id" :product="product" />
    </div>
  </div>
</template>

<script setup lang="ts">
import ProductCard from '@/components/cart/ProductCard.vue'
import { useCartStore } from '@/stores/cart'
import { useProductStore } from '@/stores/product'
import { onMounted } from 'vue'

const productStore = useProductStore()
const cartStore = useCartStore()
onMounted(() => {
  productStore.fetchProducts()
})
</script>

<style scoped>
.product-list {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.page-header h1 {
  margin: 0;
  color: #333;
}

.cart-link {
  display: inline-block;
  padding: 0.5rem 1rem;
  background: #007bff;
  color: white;
  text-decoration: none;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.cart-link:hover {
  background: #0056b3;
}

.loading,
.error {
  text-align: center;
  padding: 2rem;
  font-size: 1.1rem;
}

.error {
  color: #dc3545;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 2rem;
}

@media (max-width: 768px) {
  .product-list {
    padding: 1rem;
  }

  .page-header {
    flex-direction: column;
    gap: 1rem;
    text-align: center;
  }

  .products-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
}
</style>
